package com.witdata.mysql.base.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.witdata.mysql.base.service.UserLearningSessionService;
import com.witdata.mysql.domain.entity.TUser;
import com.witdata.mysql.domain.entity.UserLearningSession;

import com.witdata.mysql.mapper.UserLearningSessionMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;


import java.util.List;


@Service
@RequiredArgsConstructor
public class UserLearningSessionServiceImpl extends ServiceImpl<UserLearningSessionMapper, UserLearningSession> implements UserLearningSessionService {

    private final UserLearningSessionMapper userLearningSessionMapper;
    private final HttpServletResponse httpServletResponse;

    @Override
    public TUser selectByUserName(String username) {
        return userLearningSessionMapper.selectByUserName(username);
    }

    @Override
    public void saveUserName(long id,String username, long startTime,long lastTime) {
        userLearningSessionMapper.saveUserName(id,username,startTime,lastTime);
    }

    @Override
    public void saveTimeByUserName(String username,long startTime, long lastTime) {
        userLearningSessionMapper.saveTimeByUserName(username,startTime,lastTime);
    }

    @Override
    public UserLearningSession findByUsername(String username) {
        return userLearningSessionMapper.findByUserName(username);
    }

    @Override
    public List<UserLearningSession> getAllUserActivities(String userName) {
        List<UserLearningSession> list = userLearningSessionMapper.getAllUserActivities(userName);
        return list;
    }

    @Override
    public void updateStatus(String username) {
        userLearningSessionMapper.updateStatus(username);
    }

    @Override
    public List<UserLearningSession> selectAll() {
        return userLearningSessionMapper.selectAllInfo();
    }

    @Override
    public TUser findUserByUserName(String username) {
        return userLearningSessionMapper.findUserIdByUserName(username);
    }

    /**
     * 更新用户的最后活跃时间
     */
    public void updateUserActivity(String username) {
        UserLearningSession userActivity = userLearningSessionMapper.findByUserName(username);
        if (userActivity != null) {
//            userActivity.setLastActivityTime(System.currentTimeMillis()); // 更新最后活跃时间
            userLearningSessionMapper.save(userActivity);
            if(!isUserInactive(username)){
//                System.out.println("活跃中");
                userActivity.setStatus("ACTIVE");
                userLearningSessionMapper.save(userActivity);
            }else{
//                System.out.println("不活跃");
                userActivity.setStatus("INACTIVE");
                userLearningSessionMapper.save(userActivity);
                //计算总时长，打回重新登录
//                System.out.println(userActivity.getTotalTime()+userActivity.getLastActivityTime()-userActivity.getStartTime());
                userActivity.setTotalTime(userActivity.getTotalTime()+userActivity.getLastActivityTime()-userActivity.getStartTime());
                userLearningSessionMapper.save(userActivity);
//                System.out.println("重新登录...");
            }

        } else {
            // 如果没有记录，创建新的记录
            userActivity = new UserLearningSession();
            userActivity.setUsername(username);
            userActivity.setLastActivityTime(System.currentTimeMillis());
            userActivity.setStatus("ACTIVE");
            userLearningSessionMapper.save(userActivity);

        }
    }

    /**
     * 检查用户是否活跃
     * @return 用户是否活跃
     */
    public boolean isUserInactive(String userName) {
        UserLearningSession userActivity = userLearningSessionMapper.findByUserName(userName);
        if (userActivity != null) {
            long currentTime = System.currentTimeMillis();
            //多少秒不活跃
//            System.out.println("多少秒不活跃" );
            System.out.println(currentTime - userActivity.getLastActivityTime());
//            return (currentTime - userActivity.getLastActivityTime()) > 600000;  // 10分钟未活跃则认为不活跃
//            return (currentTime - userActivity.getLastActivityTime()) > 10000;  // 10秒未活跃则认为不活跃
            return (currentTime - userActivity.getLastActivityTime()) > 72000000;  // 10秒未活跃则认为不活跃
        }
        return true;  // 默认不活跃
    }
}